require('./employee.scss');

const pop = require('../../../mod/pop');
const Ajax = require('../../../mod/webajax');
const Table = require('../../../mod/table');
const Reg = require('../../../mod/reg');
const ErrorTip = require('../../../mod/error');
const Page = require('../../../mod/page');
require('../../../mod/dropdown');

let nowPage = 1;

initEmployee();

async function initEmployee(){

  try{
    await createTable(nowPage);
  }catch(e){
    // alert('获取列表emloyee有误');
  }

  $('#J_NewEmployee').click( ()=>{
    cleanForm();
    $('#employeeModal').attr('data-type', 0); // 表示弹框是新增人员
    $('#employeeModal .modal-title').html('新建人员');
    pop('employeeModal');
  } );


  AddEmployee();

}

// 新建人员
function AddEmployee(){
  $('#employee_buttonid').show(); // 保存按钮

  $('#employee_buttonid').click( async function(){

    // 中文姓名
    let user_name = $('#user_name').val().trim();
    if( user_name == '' ){
        ErrorTip($('#add_employee_error'), '中文姓名不能为空', $('#user_name').parent() );
        return;
    }

    // 英文姓名
    let acct_name = $('#acct_name').val().trim();
    if( acct_name == '' ){
        ErrorTip($('#add_employee_error'), '英文姓名不能为空', $('#acct_name').parent() );
        return;
    }

    // 手机号码
    let mobile = $('#mobile').val().trim();
    if( mobile == '' ){
        ErrorTip($('#add_employee_error'), '手机号码不能为空', $('#mobile').parent() );
        return;
    }

    if( !Reg.phone.test(mobile) ){
      ErrorTip($('#add_employee_error'), '手机号码不正确, 11位数字', $('#mobile').parent());
      return;
    }

    // 身份证
    let id_number = $('#id_number').val().trim();
    if( id_number == '' ){
        ErrorTip($('#add_employee_error'), '身份证不能为空', $('#id_number').parent() );
        return;
    }

    if( !Reg.id_number.test(id_number) ){
      ErrorTip($('#add_employee_error'), '身份证输入有误', $('#id_number').parent());
      return;
    }


    $(this).attr('disabled', true);

    let add_url = '/employee/add', edit_url = '/employee/edit', del_url='/employee/del';
    let url = add_url;
    let $_POST = {
      user_name,
      acct_name,
      mobile,
      id_number,
    };

    if( $('#employeeModal').attr('data-type') == 1  ){
      url = edit_url;
      $_POST.user_id = $('#employee_buttonid').attr('data-user-id');
    }

   try{
    let json = await Ajax({
        url,
        type:'POST',
        data:$_POST,
      });

      $(this).attr('disabled', false);

      if( json.result != 'error' ){
        ErrorTip($('#add_employee_error'), '操作成功√' );
        pop('employeeModal', false);

        await createTable(nowPage);

      }else{
        ErrorTip($('#add_employee_error'), json.msg, $('#employeephone').parent() );
      }

    }catch(e){
      $(this).attr('disabled', false);
      ErrorTip($('#add_employee_error'), json.msg );
    }

  } );

}

// 生成员工列表
async function createTable(){

  async function _table(){
    let json = await Ajax({
        url:`/employee/employee/${nowPage}/2`,
    });

    let configArr = ['user_name', 'acct_name', 'mobile', 'id_number', 'dept_role', 'operate'];

    let dataArr = json.content.rows.map( item=>{
      item['dept_role'] = "--";
      item['operate'] = `<a href="javascript:;" data-type='1' data-json='${JSON.stringify(item)}' >查看</a>
      <a href="javascript:;" data-type='2' data-json='${JSON.stringify(item)}' >编辑</a>
      <a href="javascript:;" data-type='3' data-json='${JSON.stringify(item)}' >删除</a>`;
      return item;
    } );
    Table($('table'), dataArr, configArr);

    return json;
  }

  try{
    // let json = await Ajax({
    //     url:`/employee/employee/${nowPage}/2`,
    // });
    //
    // let configArr = ['user_name', 'acct_name', 'mobile', 'id_number', 'dept_role', 'operate'];
    //
    // let dataArr = json.content.rows.map( item=>{
    //   item['dept_role'] = "--";
    //   item['operate'] = `<a href="javascript:;" data-type='1' data-json='${JSON.stringify(item)}' >查看</a>
    //   <a href="javascript:;" data-type='2' data-json='${JSON.stringify(item)}' >编辑</a>
    //   <a href="javascript:;" data-type='3' data-json='${JSON.stringify(item)}' >删除</a>`;
    //   return item;
    // } );
    // Table($('table'), dataArr, configArr);

    let json = await _table();
    // alert(9);
    bindTableAction();
    // alert(11);
    // alert(json.content.totalCount)
    // 分页
    Page( {totalCount:json.content.totalCount, complete(now){
      // alert(now);
      nowPage = now;
      _table();

    }} );

    // alert(89);

  }catch(e){
    // alert('获取列表emloyee有误');
  }


}

function bindTableAction(){
  $('table tr td a').click( function(){
    // console.log($(this).attr('data-json'))
    $('#employee_buttonid').show(); // 保存按钮

    let json_str = $(this).attr('data-json');
    let json = JSON.parse(json_str);
    let type_act = +$(this).attr('data-type');
    cleanForm();
    pop('employeeModal');

    if( type_act == 1 || type_act == 2 ){
      // 查看，编辑
      $("#user_name").val(json['user_name']);
      $("#acct_name").val(json['acct_name']);
      $("#mobile").val(json['mobile']);
      $("#id_number").val(json['id_number']);
    }

    if( type_act == 1 ){
      $('#employeeModal input').attr('disabled', true);
      $('#employeeModal .modal-title').html('查看人员');
      $('#employee_buttonid').hide(); // 保存按钮
    }

    if( type_act == 2 ){
      $('#employeeModal').attr('data-type', 1); // 表示弹框是编辑人员
      $('#employeeModal .modal-title').html('编辑人员');
      $('#employeeModal input').attr('disabled', false);
      $('#employee_buttonid').attr('data-user-id', json.user_id);
    }

  } );
}

function cleanForm() {
    $("#user_name").val("");
    $("#acct_name").val("");
    $("#mobile").val("");
    $("#id_number").val("");
    // $("#mobile").val("");
}
